package encome.work.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import encome.work.dao.Permission;
import encome.work.mapper.PermissionMapper;
import encome.work.service.PermissionService;
import encome.work.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RedisUtils redisUtil;

    @Override
    public Result findPage(QueryInfo queryInfo) {
        Page<Permission> permissionPage = new Page<>(queryInfo.getPageNumber(), queryInfo.getPageSize());
        permissionMapper.findPage(permissionPage, queryInfo.getQueryString());
        return Result.success("分页查询成功", permissionPage);
    }

    @Override
    public Result insert(Permission permission) {
        permissionMapper.insert(permission);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("添加权限数据成功");
    }

    @Override
    public Result delete(Long id) {
        permissionMapper.deleteById(id);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("删除权限数据成功");
    }

    @Override
    public Result update(Permission permission) {
        permissionMapper.updateById(permission);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("修改权限数据成功");
    }

    @Override
    public Result findAll() {
        List<Permission> list = this.list();
        return Result.success("查询权限信息成功", list);
    }


}
